package com.example.uitest

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

/**
 *@author cy
 *@date 2024/7/24
 *@apiNote
 */
//class FruitAdapter(activity: Activity, val resourceId: Int, data: List<Fruit>) :
//    ArrayAdapter<Fruit>(activity, resourceId, data) {
//    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
//        val view: View
//        val viewHolder: ViewHolder
//        if (convertView == null) {
//            view = LayoutInflater.from(context).inflate(resourceId, parent, false)
//            viewHolder = ViewHolder(view)
//            view.tag = viewHolder
////            viewHolder.textView.setOnClickListener {
////                var item = getItem(position)
////                if (item != null) {
////                    Toast.makeText(context, "${item.name}", Toast.LENGTH_LONG).show()
////                }
////            }
//
//
//        } else {
//            view = convertView
//            viewHolder = view.tag as ViewHolder
//
//        }
//
//
//        var item = getItem(position)
//        view
//        if (item != null) {
//            viewHolder.imageView.setImageResource(item.imageId)
//            viewHolder.textView.text = item.name
//
//        }
//        return view
//
//    }
//
//    inner class ViewHolder(view: View) {
//        var imageView = view.findViewById<ImageView>(R.id.fruitImage)
//        var textView = view.findViewById<TextView>(R.id.fruitName)
//    }
//
//}
class FruitAdapter(val fruitList: List<Fruit>) :
    RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val fruitImage: ImageView = view.findViewById(R.id.fruitImage)
        val fruitName: TextView = view.findViewById(R.id.fruitName)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.fruit_item, parent, false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val fruit = fruitList[position]
            Toast.makeText(parent.context, "you clicked view ${fruit.name}",
                Toast.LENGTH_SHORT).show()
        }
        viewHolder.fruitImage.setOnClickListener {
            val position = viewHolder.adapterPosition
            val fruit = fruitList[position]
            Toast.makeText(parent.context, "you clicked image ${fruit.name}",
                Toast.LENGTH_SHORT).show()
        }
        return viewHolder
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = fruitList[position]
        holder.fruitImage.setImageResource(fruit.imageId)
        holder.fruitName.text = fruit.name
    }
    override fun getItemCount() = fruitList.size
}